<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="订单编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="区域" prop="agentCode">
        <el-select v-model="queryParams.agentCode" placeholder="请选择" clearable>
          <el-option
            v-for="agent in agentList"
            :key="agent.code"
            :label="agent.name"
            :value="agent.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择" clearable>
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MEIJIA_STORE_ORDER_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="门店订单状态" prop="statusArr">
        <el-select v-model="queryParams.statusArr" placeholder="请选择" clearable multiple>
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MEIJIA_STORE_ORDER_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付完成时间" prop="payFinishTime">
        <el-date-picker
          v-model="queryParams.payFinishTime"
          class="!w-240px"
          end-placeholder="结束日期"
          start-placeholder="开始日期"
          type="daterange"
          value-format="YYYY-MM-DD HH:mm:ss"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="订单编号" align="center" prop="code" />
      <el-table-column label="订单类型" align="center" prop="orderType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MEIJIA_STORE_ORDER_TYPE" :value="scope.row.orderType" />
        </template>
      </el-table-column>
      <el-table-column label="所属区域" align="center" prop="agentName" />
      <el-table-column label="所属门店" align="center" prop="storeName" />
      <el-table-column label="门店人员" align="center" prop="userName" />
      <el-table-column label="实收总金额（元）" align="center" prop="actualPaymentCNY" />
      <el-table-column label="客户昵称" align="center" prop="memberName" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MEIJIA_STORE_ORDER_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="下单时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="200px"
      />
      <el-table-column
        label="支付完成时间"
        align="center"
        prop="payFinishTime"
        :formatter="dateFormatter"
        width="200px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="openDrawer(scope.row.code, scope.row.orderType)">
            查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <StoreOrderDetail ref="drawerRef" />
  <VipOrderDetail ref="drawerVIPRef" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as OrderApi from '@/api/meijia/order'
import StoreOrderDetail from './storeOrderDetail.vue'
import VipOrderDetail from './vipOrderDetail.vue'
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
import * as AgentApi from '@/api/meijia/agent'
defineOptions({ name: 'OrderRefund' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  code: undefined,
  orderCode: undefined,
  status: undefined,
  remark: undefined,
  createTime: [],
  version: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OrderApi.getStorePageByUser(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
const drawerRef = ref()
const drawerVIPRef = ref()
const openDrawer = (code: string, orderType: number) => {
  if (orderType == 0) {
    drawerRef.value.open(code)
  } else {
    drawerVIPRef.value.open(code)
  }
}

const agentList = ref([])
const getAgentList = async () => {
  agentList.value = await AgentApi.getByAgentCode({})
}

/** 初始化 **/
onMounted(() => {
  getAgentList()
  getList()
})
</script>
